﻿
from Devices.Chiot.ChiotDeviceParser import ChiotDeviceParser
from Devices.Chiot.Bulb.BulbParser import BulbParser

class MatterBulbParser(BulbParser):
    def ParseFullStatus(msgValue, fullStatus):
        ret, strValue = ChiotDeviceParser.GetStrProperty(msgValue, "mac")
        if ret:
            fullStatus.mac = strValue

        ret, strValue = ChiotDeviceParser.GetStrProperty(msgValue, "firmwareVersion")
        if ret:
            fullStatus.firmwareVersion = strValue

        ret, strValue = ChiotDeviceParser.GetStrProperty(msgValue, "hardwareVersion")
        if ret:
            fullStatus.hardwareVersion = strValue

        ret, strValue = ChiotDeviceParser.GetStrProperty(msgValue, "protocol")
        if ret:
            fullStatus.protocol = strValue

        ret, strValue = ChiotDeviceParser.GetStrProperty(msgValue, "build_time")
        if ret:
            fullStatus.buildTime = strValue

        ret, value = ChiotDeviceParser.GetIntProperty(msgValue, "power_type")
        if ret:
            fullStatus.powerType = value

        ret, value = ChiotDeviceParser.GetIntProperty(msgValue, "led_driver")
        if ret:
            fullStatus.ledDriver = value

        ret, value = ChiotDeviceParser.GetIntProperty(msgValue, "interface_type")
        if ret:
            fullStatus.interfaceType = value

        BulbParser.GetCurrents(msgValue, "currents", fullStatus.currents)

        if not "stateValue" in msgValue:
            return True
        stateValue = msgValue["stateValue"]

        ret, value = ChiotDeviceParser.GetIntProperty(stateValue, "switch", "on")
        if ret:
            fullStatus.switch_state = value

        ret, value = ChiotDeviceParser.GetIntProperty(stateValue, "noDisturb", "on")
        if ret:
            fullStatus.noDisturb = value

        ret, value = ChiotDeviceParser.GetIntProperty(stateValue, "lightMode", "status")
        if ret:
            fullStatus.lightAttr.lightMode = value

        ret, value = ChiotDeviceParser.GetIntProperty(stateValue, "rgbBrightness", "brightness")
        if ret:
            fullStatus.lightAttr.rgb.rgbBrightness = value

        ret, value = ChiotDeviceParser.GetIntProperty(stateValue, "whiteBrightness", "brightness")
        if ret:
            fullStatus.lightAttr.white.whiteBrightness = value

        ret, value = ChiotDeviceParser.GetIntProperty(stateValue, "saturation", "ratio")
        if ret:
            fullStatus.lightAttr.rgb.saturation = value

        ret, value = ChiotDeviceParser.GetIntProperty(stateValue, "colour", "red")
        if ret:
            fullStatus.lightAttr.rgb.red = value

        ret, value = ChiotDeviceParser.GetIntProperty(stateValue, "colour", "green")
        if ret:
            fullStatus.lightAttr.rgb.green = value

        ret, value = ChiotDeviceParser.GetIntProperty(stateValue, "colour", "blue")
        if ret:
            fullStatus.lightAttr.rgb.blue = value

        ret, value = ChiotDeviceParser.GetIntProperty(stateValue, "colorTemperature", "ratio")
        if ret:
            fullStatus.lightAttr.white.temprature = BulbParser.TemperatureRate(value)
        else:
            ret, value = ChiotDeviceParser.GetIntProperty(stateValue, "colorTemperature", "value")
            if ret:
                fullStatus.lightAttr.white.temprature = BulbParser.TemperatureRate(value)

        return True
